﻿using CPF;
using CPF.Animation;
using CPF.Charts;
using CPF.Controls;
using CPF.Drawing;
using CPF.Shapes;
using CPF.Styling;
using CPF.Svg;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace CPFUI.IControl
{
    [CPF.Design.DesignerLoadStyle("res://$safeprojectname$/Stylesheet1.css")]//用于设计的时候加载样式
    public class MacMenuItem : Control
    {
        Picture Pic;
        public MacMenuItem()
        {

            Pic = new Picture
            {
                MarginTop = 0,
                MarginLeft = 0,
                MarginRight = 0,
                MarginBottom = 2,
                IsAntiAlias = true,
                Source = Icon,
                Bindings =
                {
                    {
                        nameof(Picture.Source),
                        nameof(Icon),
                        this
                    }
                },
                Stretch = Stretch.Fill,
            };
        }
        public Color TitleBackground
        {
            get { return GetValue<Color>(); }
            set
            {
                SetValue(value);
            }
        }
        public MacMenuShowType ShowType
        {
            get { return (MacMenuShowType)GetValue(); }
            set
            {
                SetValue(value);
                switch (value)
                {
                    case MacMenuShowType.Top:
                        Pic.MarginTop = 2;
                        Pic.MarginLeft = 0;
                        Pic.MarginRight = 0;
                        Pic.MarginBottom = 40;
                        break;
                    case MacMenuShowType.Right:
                        Pic.MarginTop = 0;
                        Pic.MarginLeft = 40;
                        Pic.MarginRight = 2;
                        Pic.MarginBottom = 0; 
                        break;
                    case MacMenuShowType.Botton:

                        Pic.MarginTop = 40;
                        Pic.MarginLeft = 0;
                        Pic.MarginRight = 0;
                        Pic.MarginBottom =2 ; 
                        break;
                    case MacMenuShowType.Left:
                        Pic.MarginTop = 0;
                        Pic.MarginLeft = 2;
                        Pic.MarginRight = 40;
                        Pic.MarginBottom = 0;
                        break;

                }
            }
        }
        public bool IsDown = false;
        /// <summary>
        /// 标题
        /// </summary>
        [Description("标题")]
        public string Caption
        {
            get { return (string)GetValue(); }
            set { SetValue(value); }
        }
        public Visibility IsEnter
        {
            get { return (Visibility)GetValue(); }
            set { SetValue(value);
                if (value == Visibility.Hidden)
                {
                    IsEnterBottom = value;
                    IsEnterLeft = value;
                    IsEnterTop = value;
                    IsEnterRight = value;
                }
                switch (ShowType)
                {
                    case MacMenuShowType.Top:
                        IsEnterBottom = value;
                        break;
                    case MacMenuShowType.Right:
                        IsEnterLeft = value;
                        break;
                    case MacMenuShowType.Botton:

                        IsEnterTop = value;
                        break;
                    case MacMenuShowType.Left:
                        IsEnterRight = value;
                        break;

                }
            }
        }
        [Description("显示隐藏"), DefaultValue(Visibility.Hidden)]
        public Visibility IsEnterTop
        {
            get { return (Visibility)GetValue(); }
            set { SetValue(value); }
        }
        [Description("显示隐藏"), DefaultValue(Visibility.Hidden)]
        public Visibility IsEnterBottom
        {
            get { return (Visibility)GetValue(); }
            set { SetValue(value); }
        }
        [Description("显示隐藏"), DefaultValue(Visibility.Hidden)]
        public Visibility IsEnterLeft
        {
            get { return (Visibility)GetValue(); }
            set { SetValue(value); }
        }
        [Description("显示隐藏"), DefaultValue(Visibility.Hidden)]
        public Visibility IsEnterRight
        {
            get { return (Visibility)GetValue(); }
            set { SetValue(value); 
            }
        }
        /// <summary>
        /// 标题颜色
        /// </summary>
        [Description("标题颜色")]
        public string CaptionColor
        {
            get { return (string)GetValue(); }
            set { SetValue(value); }
        }
        /// <summary>
        /// 图标
        /// </summary>
        [Description("图标")]
        public object Icon
        {
            get { return (object)GetValue(); }
            set { SetValue(value); }
        }
        [Description("Item类型"),DefaultValue(MacMenuItemType.None)]
        public MacMenuItemType MenuItemType
        {
            get { return (MacMenuItemType)GetValue(); }
            set { SetValue(value); }
        }
        //模板定义
        protected override void InitializeComponent()
        {
            
            Cursor = Cursors.Hand;
            TitleBackground = Color.FromRgb(222, 222, 222);

            Children.Add(Pic );
            ShowType = MacMenuShowType.Botton;
            AddTopTitleControl();
            AddBottomTitleControl();
            AddLeftTitleControl();
            AddRightTitleControl();
        }
        public void AddTopTitleControl()
        {
            Children.Add(
                new Panel
                {
                    IsAntiAlias = true,
                    MarginTop = 0,
                    Height = 40,
                    Children =
                {
                    new Border
                    {
                        BorderType = BorderType.BorderStroke,
                        BorderStroke = new Stroke(0.3f),
                        BorderFill=Color.Gray,
                        Background=TitleBackground,
                        ShadowBlur=0,
                        MarginBottom=15,
                        Height=25,
                        ShadowColor=Color.Gray,
                        CornerRadius="4",
                        Bindings={
                            {
                                nameof(Border.Background),
                                nameof(TitleBackground),
                                this,
                                BindingMode.TwoWay
                            }
                        },
                        Child=new Label
                        {
                            Text = "标题",
                            Bindings =
                            {
                                {
                                    nameof(Label.Text),
                                    nameof(Caption),
                                    this
                                }
                            },
                            MarginTop= 4,
                            MarginBottom=4,
                            MarginLeft = 10,
                            MarginRight = 10,
                        }
                    },
                    new Panel
                    {
                        MarginBottom=10,
                        Children =
                        {
                            new Polygon
                            {
                                Points =
                                {
                                    new Point(0, 0),
                                    new Point(6, 6),
                                    new Point(12, 0)
                                },

                                StrokeStyle = "0.3",
                                Fill=TitleBackground,
                                StrokeFill=Color.Gray,
                                Bindings={
                                    {
                                        nameof(Polygon.Fill),
                                        nameof(TitleBackground),
                                        this,
                                        BindingMode.TwoWay
                                    }
                                },
                            },

                            new Line
                            {
                                Width=14,
                                Height=6,
                                StartPoint= new Point(0f, 0),
                                EndPoint= new Point(14, 0),
                                StrokeFill=TitleBackground,
                                StrokeStyle = "2",
                                Bindings={
                                    {
                                        nameof(Line.StrokeFill),
                                        nameof(TitleBackground),
                                        this,
                                        BindingMode.TwoWay
                                    }
                                },
                            },
                        }
                    }
                },
                    Visibility=IsEnterTop,
                    Bindings =
                {
                    {
                        nameof(Panel.Visibility),
                        nameof(IsEnterTop),
                        this
                    }
                },
                });
        }
        public void AddBottomTitleControl()
        {
            Children.Add(
                new Panel
                {
                    IsAntiAlias = true,
                    MarginBottom = 0,
                    Height = 40,
                    Children =
                {
                    new Border
                    {
                        BorderType = BorderType.BorderStroke,
                        BorderStroke = new Stroke(0.3f),
                        BorderFill=Color.Gray,
                        Background=TitleBackground,
                        ShadowBlur=0,
                        MarginTop=15,
                        Height=25,
                        ShadowColor=Color.Gray,
                        CornerRadius="4",
                        Bindings={
                            {
                                nameof(Border.Background),
                                nameof(TitleBackground),
                                this,
                                BindingMode.TwoWay
                            }
                        },
                        Child=new Label
                        {
                            Text = "标题",
                            Bindings =
                            {
                                {
                                    nameof(Label.Text),
                                    nameof(Caption),
                                    this
                                }
                            },
                            MarginTop= 4,
                            MarginBottom=4,
                            MarginLeft = 10,
                            MarginRight = 10,
                        }
                    },
                    new Panel
                    {
                        MarginTop=10,
                        Children =
                        {
                            new Polygon
                            {
                                Points =
                                {
                                    new Point(6, 0),
                                    new Point(0, 6),
                                    new Point(12, 6)
                                },

                                StrokeStyle = "0.3",
                                Fill=TitleBackground,
                                StrokeFill=Color.Gray,
                                Bindings={
                                    {
                                        nameof(Polygon.Fill),
                                        nameof(TitleBackground),
                                        this,
                                        BindingMode.TwoWay
                                    }
                                },
                            },

                            new Line
                            {
                                Width=14,
                                Height=6,
                                StartPoint= new Point(0, 6.3f),
                                EndPoint= new Point(13, 6.3f),
                                StrokeFill=TitleBackground,
                                StrokeStyle = "2",
                                Bindings={
                                    {
                                        nameof(Line.StrokeFill),
                                        nameof(TitleBackground),
                                        this,
                                        BindingMode.TwoWay
                                    }
                                },
                            },
                        }
                    }
                },
                    Visibility = IsEnterBottom,
                    Bindings =
                {
                    {
                        nameof(Panel.Visibility),
                        nameof(IsEnterBottom),
                        this
                    }
                },
                });
        }
        public void AddLeftTitleControl()
        {
            Children.Add(
                new Panel
                {
                    IsAntiAlias = true,
                    Width = 40,
                    MarginLeft =0,
                    Children =
                {
                    new Border
                    {
                        BorderType = BorderType.BorderStroke,
                        BorderStroke = new Stroke(0.3f),
                        BorderFill=Color.Gray,
                        Background=TitleBackground,
                        ShadowBlur=0,
                        MarginLeft=0,
                        Width=25,
                        ShadowColor=Color.Gray,
                        CornerRadius="4",
                        Bindings={
                            {
                                nameof(Border.Background),
                                nameof(TitleBackground),
                                this,
                                BindingMode.TwoWay
                            }
                        },
                        Child=new Label
                        {
                            Text = "标题",
                            Bindings =
                            {
                                {
                                    nameof(Label.Text),
                                    nameof(Caption),
                                    this
                                }
                            },
                            MarginTop= 10,
                            MarginBottom=10,
                            MarginLeft = 4,
                            MarginRight = 4,
                        }
                    },
                    new Panel
                    {
                        MarginRight=10,
                        Children =
                        {
                            new Polygon
                            {
                                Points =
                                {
                                    new Point(0, 0),
                                    new Point(6, 6),
                                    new Point(0, 12)
                                },

                                StrokeStyle = "0.3",
                                Fill=TitleBackground,
                                StrokeFill=Color.Gray,
                                Bindings={
                                    {
                                        nameof(Polygon.Fill),
                                        nameof(TitleBackground),
                                        this,
                                        BindingMode.TwoWay
                                    }
                                },
                            },

                            new Line
                            {
                                Width=6,
                                Height=14,
                                StartPoint= new Point(0, 0),
                                EndPoint= new Point(0, 14f),
                                StrokeFill=TitleBackground,
                                StrokeStyle = "2",
                                Bindings={
                                    {
                                        nameof(Line.StrokeFill),
                                        nameof(TitleBackground),
                                        this,
                                        BindingMode.TwoWay
                                    }
                                },
                            },
                        }
                    }
                },
                    Visibility = IsEnterLeft,
                    Bindings =
                {
                    {
                        nameof(Panel.Visibility),
                        nameof(IsEnterLeft),
                        this
                    }
                },
                });
        }
        public void AddRightTitleControl()
        {
            Children.Add(
                new Panel
                {
                    IsAntiAlias = true,
                    Width = 40,
                    MarginRight = 0,
                    Children =
                {
                    new Border
                    {
                        BorderType = BorderType.BorderStroke,
                        BorderStroke = new Stroke(0.3f),
                        BorderFill=Color.Gray,
                        Background=TitleBackground,
                        ShadowBlur=0,
                        MarginRight=0,
                        Width=25,
                        ShadowColor=Color.Gray,
                        CornerRadius="4",
                        Bindings={
                            {
                                nameof(Border.Background),
                                nameof(TitleBackground),
                                this,
                                BindingMode.TwoWay
                            }
                        },
                        Child=new Label
                        {
                            Text = "标题",
                            Bindings =
                            {
                                {
                                    nameof(Label.Text),
                                    nameof(Caption),
                                    this
                                }
                            },
                            MarginTop= 10,
                            MarginBottom=10,
                            MarginLeft = 4,
                            MarginRight = 4,
                        }
                    },
                    new Panel
                    {
                        MarginLeft=10,
                        Children =
                        {
                            new Polygon
                            {
                                Points =
                                {
                                    new Point(6, 0),
                                    new Point(0, 6),
                                    new Point(6, 12)
                                },

                                StrokeStyle = "0.3",
                                Fill=TitleBackground,
                                StrokeFill=Color.Gray,
                                Bindings={
                                    {
                                        nameof(Polygon.Fill),
                                        nameof(TitleBackground),
                                        this,
                                        BindingMode.TwoWay
                                    }
                                },
                            },

                            new Line
                            {
                                Width=6,
                                Height=14,
                                StartPoint= new Point(6.3f, 0),
                                EndPoint= new Point(6.3f, 14f),
                                StrokeFill=TitleBackground,
                                StrokeStyle = "2",
                                Bindings={
                                    {
                                        nameof(Line.StrokeFill),
                                        nameof(TitleBackground),
                                        this,
                                        BindingMode.TwoWay
                                    }
                                },
                            },
                        }
                    }
                },
                    Visibility = IsEnterRight,
                    Bindings =
                {
                    {
                        nameof(Panel.Visibility),
                        nameof(IsEnterRight),
                        this
                    }
                },
                });
        }
#if !DesignMode //用户代码写到这里，设计器下不执行，防止设计器出错
        protected override void OnInitialized()
        {
            base.OnInitialized();

        }
        //用户代码

#endif
    }
}
